(() => {
    // jQuery检查
    if (typeof jQuery == "undefined") {
        console.error("This page does NOT contain JQuery,MCBBS Extender will not work.");
        return false;
    }
    //在手机页面主动禁用
    if (document.getElementsByTagName('meta').viewport) {
        console.log("MCBBS Extender not fully compatible with Moblie page,exit manually");
        return false;
    }

    // 基本信息初始化
    let version = "v1.7.0"
    let vercode = 111740
    let updatelist = [
        '1.优化 阻止自动播放功能',
        '2.新增 Flash播放器转H5功能',
        '3.新增 一键彩虹字体功能,选中文字并点击彩虹按钮即可将文字彩虹化.',
        '4.修复 代码块美化在翻页中行计数器消失的问题'
    ];
    let configableList = [
        {
            "id": "fixCodeBlock",
            "default": true,
            "type": "check",
            "name": "美化代码块样式",
            "desc": "修正代码块的一些样式,如滚动条."
        }, {
            "id": "queryMessage",
            "default": true,
            "type": "check",
            "name": "后台轮询消息",
            "desc": "在后台自动查询是否有新的消息并推送,需保证至少打开一个页面."
        }, {
            "id": "queryMessageInterval",
            "default": 60,
            "type": "num",
            "name": "后台轮询消息间隔",
            "desc": "两次轮询消息之间的间隔,单位秒.注意,过低的值可能会导致你被论坛屏蔽,超过200的值可能会导致消息反复推送."
        }, {
            "id": "rememberPage",
            "default": true,
            "type": "check",
            "name": "板块内翻页记忆",
            "desc": "点击板块内下一页按钮时记忆当前页."
        }, {
            "id": "animateGoToTopButton",
            "default": true,
            "name": "回到顶部按钮美化",
            "type": "check",
            "desc": "为右侧回到顶部按钮增加动画以及位置修正."
        }, {
            "id": "pinnedTopBar",
            "default": true,
            "name": "更好的固定顶栏",
            "type": "check",
            "desc": "优化固定顶栏的行为,如与编辑栏的兼容性,以及在极窄窗口下的显示."
        }, {
            "id": "fixTopBarPopMenu",
            "default": true,
            "type": "check",
            "name": "弹出菜单美化",
            "desc": "美化弹出菜单的样式,如个人信息菜单."
        }, {
            "id": "hoverableMedal",
            "default": true,
            "name": "玻璃质感勋章",
            "type": "check",
            "desc": "亮闪闪的勋章~"
        }, {
            "id": "viewWarns",
            "default": true,
            "name": "查看警告记录",
            "type": "check",
            "desc": "为每一层楼和每一个个人主页(除自己)添加查看警告记录按钮"
        }, {
            "id": "removeLinkWarn",
            "default": true,
            "name": "移除外链警告",
            "type": "check",
            "desc": "去除论坛跳转外链时的警告页面."
        }, {
            "id": "useIgInQuickReply",
            "default": true,
            "name": "快速回复使用个人签名",
            "type": "check",
            "desc": "在页脚快速回复帖子时使用个人签名."
        }, {
            "id": "fixImgZoom",
            "default": true,
            "name": "优化图片缩放",
            "type": "check",
            "desc": "使用更现代的方法实现图片缩放."
        }, {
            "id": "disableAutoplay",
            "default": false,
            "name": "禁止BGM自动播放",
            "type": "check",
            "desc": "阻止页内BGM自动播放."
        }, {
            "id": "remenberEditMode",
            "default": true,
            "name": "记忆编辑器模式",
            "type": "check",
            "desc": "记忆高级编辑器是纯文本模式还是即时模式."
        }, {
            "id": "highlightThreads",
            "default": true,
            "name": "帖子列表高亮",
            "type": "check",
            "desc": "列表高亮显示帖子类型."
        }, {
            "id": "fixAnchor",
            "default": false,
            "name": "帖内锚点修复",
            "type": "check",
            "desc": "防止帖内锚点被意外的赋予样式."
        }, {
            "id": "replaceFlash",
            "default": true,
            "name": "Flash播放器替换",
            "type": "check",
            "desc": "将网易云Flash播放器替换成H5播放器."
        },{
            "id": "quickRainbow",
            "default": true,
            "name": "编辑器支持彩虹文字",
            "type": "check",
            "desc": "快速向贴内插入彩虹文字. —— \"虽然世界灰暗,但是我心里有彩虹呀.\""
        }, {
            "id": "maxMedalLine",
            "default": -1,
            "type": "num",
            "name": "最大勋章行数",
            "desc": "限制楼层勋章的最大行数,提升鼠标滚轮寿命,设置为-1以禁用此功能."
        }, {
            "id": "quickAtList",
            "default": "",
            "name": "快速 @ 列表",
            "type": "text",
            "desc": "按下Ctrl+Shift+A/或者按钮以快速在当前输入框内插入预定义的@用户名代码.用户名之间用\",\"(半角逗号)分隔."
        }, {
            "id": "miscFix",
            "default": "",
            "name": "杂项修复",
            "type": "text",
            "desc": "此值用于规定杂项修复的行为,默认值为空,修改为00000000000以关闭全部.错误的值会使该项失效.详情请查阅源码."
        }, {
            "id": "myReportReason",
            "default": "",
            "name": "自定义举报理由",
            "type": "textarea",
            "desc": "在举报时提供自定义的举报理由,一行一个理由."
        }, {
            "id": "myCSS",
            "default": "",
            "name": "自定义CSS",
            "type": "textarea",
            "desc": "在框内的CSS会被加载到页面内,可自由发挥."
        }, {
            "id": "myLinks",
            "default": "",
            "name": "自定义工具菜单链接",
            "type": "textarea",
            "desc": "在右上角\"工具\"菜单里添加自定义的链接,以\"[名称] [链接]\"的格式添加(如\"个人主页 home.php\"),一行一个."
        }];
    //夹带私货
    console.log(" %c Zapic's Homepage %c https://i.zapic.cc ", "color: #ffffff; background: #E91E63; padding:5px;", "background: #000; padding:5px; color:#ffffff");
    //初始化jQuery和基本封装方法
    let $ = jQuery;
    let dlg = (m) => {
        console.debug("[MCBBS Extender]" + m);
    };
    let getRequest = (variable, url = "") => {
        let query = url ? /\?(.*)/.exec(url)[1] : window.location.search.substring(1);
        let vars = query.split("&");
        for (let i = 0; i < vars.length; i++) {
            let pair = vars[i].split("=");
            if (pair[0] == variable) {
                return pair[1];
            }
        }
        return (false);
    }
    //内建静态资源
    let staticRes = {
        "atBtnImage": "",
        "medalReflectImage": "",
        "rainbowBtnImage":""
    };
    //配置数据迁移函数
    let transferConfig = () => {
        let tempconf = null;
        try {
            tempconf = JSON.parse(getcookie("MExt_config"));
            if (typeof tempconf.version == "undefined") { return false; } else {
                localStorage.setItem("MExt_config", JSON.stringify(tempconf));
                setcookie("MExt_config", "", 0);
            }
        } catch (e) {
            return false;
        }
    }
    //配置初始化函数
    let initConfig = () => {
        $(configableList).each((i, v) => {
            conf[v.id] = typeof conf[v.id] == "undefined" ? v.default : conf[v.id];
        });
        localStorage.setItem("MExt_config", JSON.stringify(conf));
    }
    let conf = '';
    try {
        transferConfig();
        conf = JSON.parse(localStorage.getItem("MExt_config"));
    } catch (e) {
        dlg("Failed to load config\n" + e);
    }
    if (!conf) {
        conf = {};
        conf.version = vercode;
        initConfig();
        showDialog("<b>欢迎使用MCBBS Extender</b>.<br>本脚本的设置按钮已经放进入了您的个人信息菜单里,如需调整设置请在个人信息菜单里查看.", "right", "欢迎", () => {
            showMenu('user_info');
            $("#MExt_config").css("background-color", "#E91E63").css("color", "#fff");
            setTimeout(() => {
                hideMenu('user_info_menu');
                $("#MExt_config").css("background-color", "").css("color", "");
            }, 3000);
        });
        dlg("Config init.");
    }
    if (typeof conf.version == "undefined" || conf.version < vercode) {
        let updateContent = '';
        $(updatelist).each((i, v) => {
            updateContent += "<br>" + v;
        });
        showDialog("<b>MCBBS Extender 已经更新至 " + version + "</b>" + updateContent, "right");
        conf.version = vercode;
        initConfig();
    }

    let appendStyle = (str) => {
        $("#MExt_StyleCore")[0].innerHTML += ("\n" + str);
    }

    $("head").append('<style id="MExt_StyleCore"></style>');
    $(() => {
        // 发送警告
        if (location.pathname == "/forum.php" && getRequest('mod') == "post" && getRequest('action') == "newthread" && getRequest('fid') == "246") {
            $("body").append($(`<div id="close_script_alert" style="max-width:430px;position: fixed; left: 20px; top: 80px; z-index: 9999; transform: matrix3d(1, 0, 0, 0.0001, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1.025) translateX(-120%); background: rgba(228, 0, 0, 0.81); color: white; padding: 15px; transition-duration: 0.3s; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.66) 2px 2px 5px 0px;"><h1 style="font-size: 3em;float: left;margin-right: 12px;font-weight: 500;margin-top: 6px;">警告</h1><span style="font-size: 1.7em;">您正在向反馈与投诉版发表新的帖子</span><br>如果您正在向论坛报告论坛内的Bug,请先关闭此脚本再进行一次复现,以确保Bug不是由MCBBS Extender造成的.</div>`));
            setTimeout(() => { $("#close_script_alert")[0].style.transform = "matrix3d(1, 0, 0, 0.0001, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1.025)"; }, 10);
            setTimeout(() => { $("#close_script_alert")[0].style.transform = "none"; }, 300);
            setTimeout(() => { $("#close_script_alert")[0].style.transform = "translateX(-120%)"; }, 10000);
        }
        // 设置界面初始化
        $("#user_info_menu .user_info_menu_btn").append("<li><a href='javascript: void(0);' id=\"MExt_config\">MCBBS Extender 设置</a></li>");
        dlg("Appended Config button");
        appendStyle(
`.conf_contain {
    max-height: 45vh;
    overflow-y: auto;
    padding-right: 5px;
    overflow-x: hidden;
    scrollbar-color: rgba(0, 0, 0, 0.17) #f7f7f7;
    scrollbar-width: thin;
}

.alert_info ::-webkit-scrollbar {
    background: #f7f7f7;
    height: 7px;
    width: 7px
}

.alert_info ::-webkit-scrollbar-thumb:hover {
    background: rgba(0, 0, 0, 0.35);
}

.alert_info ::-webkit-scrollbar-thumb {
    background: rgba(0, 0, 0, 0.17);
}

.conf_item {
    line-height: 1.2;
    margin-bottom: 5px;
}

.conf_title {
    font-weight: 1000;
}

.conf_subtitle {
    font-size: 10px;
    color: rgba(0, 0, 0, 0.5);
    padding-right: 40px;
    display: block;
}

.conf_check {
    float: right;
    margin-top: -25px;
}

.conf_input {
    float: right;
    width: 30px;
    margin-top: -27px;
}

.conf_longinput {
    width: 100%;
    margin-top: 5px;
}

.conf_textarea {
    width: calc(100% - 4px);
    margin-top: 5px;
    resize: vertical;
    min-height: 50px;
}`);
        dlg("Appended Config window style");
        $("#MExt_config").on("click", () => {
            let confwinContent = '<style>body{overflow:hidden}.altw{width:700px;max-width:95vh;}.alert_info {background-image: unset;padding-left: 20px;padding-right: 17px;}</style><div class="conf_contain">';
            $(configableList).each((i, v) => {
                let inputType = '';
                switch (v.type) {
                    case "check":
                        inputType = '<input class="conf_check" type="checkbox" id="in_' + v.id + '"></input>';
                        break;
                    case "num":
                        inputType = '<input type="number" class="conf_input" id="in_' + v.id + '"></input>';
                        break;
                    case "text":
                        inputType = '<input type="text" class="conf_longinput" id="in_' + v.id + '"></input>';
                        break;
                    case "textarea":
                        inputType = '<textarea class="conf_textarea" id="in_' + v.id + '"></textarea>';
                        break;
                    default:
                        inputType = '<input class="conf_check" type="checkbox" id="in_' + v.id + '"></input>';
                        break;
                }
                confwinContent += '<p class="conf_item"><span class="conf_title">' + v.name + '</span><br><span class="conf_subtitle">' + v.desc + '</span>' + inputType + '</p>';
            });
            confwinContent += '</div>';
            showDialog(
                confwinContent,
                "confirm",
                "MCBBS Extender 设置",
                () => {
                    $(configableList).each((i, v) => {
                        let val = '';
                        if (v.type == "num" || v.type == "text" || v.type == "textarea") {
                            val = $("#in_" + v.id).val();
                        } else {
                            val = $("#in_" + v.id).prop("checked");
                        }
                        conf[v.id] = val;
                    });
                    initConfig();
                    setTimeout(() => {
                        showDialog("设置已保存,刷新生效", "right");
                    });
                },
                true,
                () => { },
                "MCBBS Extender " + version + " - <s>世界第二委屈公主殿下</s>"
            );
            $(configableList).each((i, v) => {
                if (v.type == "num" || v.type == "text" || v.type == "textarea") {
                    $("#in_" + v.id).val(conf[v.id]);
                } else {
                    $("#in_" + v.id).prop("checked", conf[v.id]);
                }
            });
            dlg("Config cookie loaded.");
        });
        dlg("Config button event attached.");
        // 钩住DiscuzAjax函数,使其触发全局事件
        let __ajaxpost = ajaxpost;
        ajaxpost = (formid, showid, waitid, showidclass, submitbtn, recall) => {
            let relfunc = () => {
                if (typeof recall == 'function') {
                    recall();
                } else {
                    eval(recall);
                }
                $(this).trigger('DiscuzAjaxPostFinished');
            }
            __ajaxpost(formid, showid, waitid, showidclass, submitbtn, relfunc);
        }
        let __ajaxget = ajaxget;
        ajaxget = (url, showid, waitid, loading, display, recall) => {
            let relfunc = () => {
                if (typeof recall == 'function') {
                    recall();
                } else {
                    eval(recall);
                }
                $(this).trigger('DiscuzAjaxGetFinished');
            }
            __ajaxget(url, showid, waitid, loading, display, relfunc);
        }
        dlg("Hooked into Discuz Ajax event");
    });
    if (conf.fixCodeBlock) {
        // 代码块美化样式
        appendStyle(
`pre:not([id]) code {
    background: #f7f7f7;
    display: block;
    font-family: Monaco, Consolas, 'Lucida Console', 'Courier New', serif;
    font-size: 12px;
    line-height: 1.8em;
    padding: 10px;
    border: #ccc solid 1px;
    position: relative;
}

.pl .blockcode ol li:hover {
    background: none;
    color: #666
}

.pl .blockcode ol li {
    white-space: nowrap;
    list-style: none;
    padding-left:0;
    margin-left:0;
}

.pl pre em, .pl .blockcode em {
    font-size: 0;
}

.pl pre em::after, .pl .blockcode em::after {
    content: 'Copy';
    position: absolute;
    top: 3px;
    right: 7px;
    display: block;
    font-size: 14px;
    border: #369 dashed 1px;
    padding: 0 7px;
    border-radius: 3px;
    transition-duration: .1s;
    opacity: 0.3;
    color: #369;
    cursor: pointer;
    font-family: Monaco, Consolas, 'Lucida Console', 'Courier New', serif;
}

.pl .blockcode,.pl pre:not([id]) {
    position: relative;
    padding: 0;
}

.pl pre em:active::after, .pl .blockcode em:active::after {
    background: #369;
    border: #369 solid 2px;
    color: white;
}

.pl .blockcode em:hover::after, .pl pre em:hover::after {
    opacity: 1;
}

.pl .blockcode div[id], pre:not([id]) code {
    max-height: 500px;
    overflow: auto;
    padding: 10px 30px 5px 50px;
    background: #F7F7F7 url(https://www.mcbbs.net/template/mcbbs/image/codebg.gif) repeat-y 0 0;
    scrollbar-width: thin;
}

.pl .blockcode div[id]::-webkit-scrollbar, pre:not([id]) code::-webkit-scrollbar {
    width: 7px;
    height: 7px;
}

.pl .blockcode div[id]::-webkit-scrollbar-thumb, pre:not([id]) code::-webkit-scrollbar-thumb {
    background: #00000040
}

.line-counter {
    position: sticky;
    float: left;
    left: -50px;
    line-height: 1.8em;
    padding-top: 3px;
    user-select: none;
    margin: -4px 0px -50px -50px;
    border-right: #d6d6d6 solid 1px;
    width: 38px;
    background: #ededed;
    font-size: 12px;
    font-family: Monaco, Consolas, 'Lucida Console', 'Courier New', serif;
    padding-right: 4px;
    text-align: right;
}

.pl .blockcode ol {
    margin: 0!important;
}

.pl .t_table .blockcode ol li {
    width:0;
}
pre:not([id]) code br{
    display: none;
}
`);

        dlg("Code block fix style appended.");
        let LnBuilder = (ln) => {
            let str = "";
            for (let i = 1; i <= ln; i++) {
                str += (i < 10 ? "0" + i.toString() : i.toString()) + ".\n";
            }
            return str;
        };
        // 为代码块添加行数显示与复制按钮
        let fixCode = () => {
            $(".pl pre:not([id]) code").each((i, v) => {
                let ln = v.innerHTML.split("\n").length;
                let lnC = LnBuilder(ln);
                let counter = document.createElement("div");
                counter.className = "line-counter";
                counter.innerText = lnC;
                let copy = document.createElement("em");
                copy.className = "code-copy";
                copy.addEventListener("click", (e) => {
                    let n = e.currentTarget.previousSibling.cloneNode(true);
                    copycode(n);
                });
                v.prepend(counter);
                v.parentElement.append(copy);
                });
            $(".pl div.blockcode").each((i, v) => {
                let ln = v.firstElementChild.firstElementChild.childElementCount;
                let lnC = LnBuilder(ln);
                let counter = document.createElement("div");
                    counter.className = "line-counter";
                    counter.innerText = lnC;
                    v.firstElementChild.prepend(counter);
            });
            copycode = (t)=>{
                    t = t.cloneNode(true);
                    t.firstElementChild.remove();
                    setCopy(t.innerText.replace(/\n\n/g, "\n"),"代码已复制到剪贴板");
            };
        }
        $(fixCode);
        $(this).on("DiscuzAjaxGetFinished DiscuzAjaxPostFinished",fixCode);
    }
    // 消息轮询
    if (conf.queryMessage) {
        // 检查消息函数
        let checkNotifica = (noNotifica = false) => {
            if (localStorage.getItem("MExt_ActiveQueryId") != queryId) {
                return false;
            }
            dlg("Checking message...");
            $.get("/forum.php?mod=misc", (d) => {
                // 设置最后通知时间为当前时间,以防止反复推送
                localStorage.setItem('notifica-time', new Date().getTime());
                let dom = $(d);
                // 获得顶栏图标类
                let noticlass = dom.find("#myprompt").attr("class");
                // 获得通知菜单元素
                let notimenu = dom.filter("#myprompt_menu");
                // 将顶栏图标类写入当前页
                $("#myprompt").attr("class", noticlass);
                // 将通知菜单写入当前页
                $("#myprompt_menu").html(notimenu.html());
                // 获得消息内容,用作缓存
                let noticontent = notimenu.html();
                // 判断是否应该发送消息
                if (!noNotifica && localStorage.getItem("MExt_LastNoticeContent") != noticontent) {
                    // 获得通知脚本(暴力)
                    let scp = dom.filter("script[src*=html5notification]").nextUntil("div").last().text();
                    // 将最后通知时间设置为1,强行启用通知
                    localStorage.setItem('notifica-time', 1);
                    // 执行通知脚本
                    eval(scp);
                    dlg("Notifica sent");
                    // 写入消息缓存
                    localStorage.setItem("MExt_LastNoticeContent", noticontent);
                    localStorage.setItem("MExt_LastNoticeCount", noticlass);
                }
            });
        }
        // 刷新消息缓存
        let flushContent = () => {
            $.get("/forum.php?mod=misc", (d) => {
                let dom = $(d);
                let noticontent = dom.filter("#myprompt_menu").html();
                let noticlass = dom.find("#myprompt").attr("class");
                // 写入消息缓存
                localStorage.setItem("MExt_LastNoticeContent", noticontent);
                localStorage.setItem("MExt_LastNoticeCount", noticlass);
            });
        }
        // 生成queryID,用于页面间的互斥
        let queryId = hash(new Date().getTime().toLocaleString(), 16);
        // 判断是否在消息页面||最后通知时间是否超过200秒
        if ((location.pathname == "/home.php" && (getRequest('do') == "pm" || getRequest('do') == "notice")) || new Date().getTime() - localStorage.getItem("notifica-time") > 200000) {
            flushContent();
        } else {
            checkNotifica();
        }
        dlg("Query id is " + queryId + ".");
        // 运行定时器,用于检查其他页面是否在运行
        setInterval(() => {
            if (localStorage.getItem("MExt_LastQuery") == "") {
                localStorage.setItem("MExt_LastQuery", 0);
            }
            let nowtime = Math.floor(new Date().getTime() / 1000);
            if ((localStorage.getItem("MExt_ActiveQueryId") == "" || nowtime - localStorage.getItem("MExt_LastQuery") > 5) && localStorage.getItem("MExt_ActiveQueryId") != queryId) {
                localStorage.setItem("MExt_ActiveQueryId", queryId);
                checkNotifica();
                dlg("Kick off inactive querier,start query.");
            }
            if (localStorage.getItem("MExt_ActiveQueryId") == queryId) {
                localStorage.setItem("MExt_LastQuery", nowtime);
            }
        }, 1000);
        dlg("Running checker actived.");
        // 判断是否有HTML5Notification
        if (!window.Html5notification) {
            $.getScript("data/cache/html5notification.js?xm6");
            dlg("Html5notification added.");
        }
        //
        $(window).on("focus", () => {
            dlg("Get content from cache");
            $("#myprompt_menu").html(localStorage.getItem("MExt_LastNoticeContent"));
            $("#myprompt").attr("class", localStorage.getItem("MExt_LastNoticeCount"));
        });
        // 定时运行检查函数
        let msgChecker = setInterval(checkNotifica, conf.queryMessageInterval * 1000);
        dlg("Message querier actived.");
    }
    if (conf.rememberPage) {
        // 记住当前页
        $(() => {
            let npbtn = $("#autopbn");
            if (npbtn.length) {
                // 绑定事件
                let orgfunc = npbtn[0].onclick;
                npbtn[0].onclick = null;
                npbtn.on("click", () => {
                    if (npbtn.html() == "正在加载, 请稍后...") { return false; }
                    let nextpageurl = npbtn.attr('rel');
                    let curpage = parseInt(npbtn.attr('curpage'));
                    npbtn.attr('curpage', curpage + 1);
                    nextpageurl = nextpageurl.replace(/&page=\d+/, '&page=' + (curpage + 1));
                    $("#threadlisttableid").append("<a class=\"mext_rempage\" rel=\"" + nextpageurl + "\"></a>")
                    //history.replaceState(null, null, nextpageurl);
                    orgfunc();
                });
                $("#separatorline").after("<a class=\"mext_rempage\" rel=\"" + window.location + "\"></a>");
                let timer = -1;
                $(window).on("scroll", () => {
                    clearTimeout(timer);
                    timer = setTimeout(() => {
                        let scroll = document.scrollingElement.scrollTop - window.innerHeight / 2;
                        let url = null;
                        document.querySelectorAll(".mext_rempage").forEach((v, i) => {
                            let vtop = v.offsetTop;
                            if (vtop < scroll || i == 0) {
                                url = v.rel;
                            }
                        });
                        if (url) {
                            history.replaceState(null, null, url);
                        }
                    }, 250);
                });
            }
            dlg("Page remember actived.");
        });
    }
    if (conf.animateGoToTopButton) {
        // 添加侧边按钮样式
        appendStyle(
`#scrolltop {
    bottom: 270px!important;
    visibility: visible;
    overflow-x: hidden;
    width: 75px;
}

.scrolltopa {
    transition-duration: .15s;
    margin-left: -40px;
    opacity: 0;
}

.scrolltopashow {
    margin-left: 0px;
    opacity: 1;
}`);
        dlg("Animate go to top buttom style appended.");
        // 重写showTopLink函数,使其使用侧边栏样式
        showTopLink = () => {
            let ft = $('#ft')[0];
            if (ft) {
                let scrolltop = $('#scrolltop')[0];
                if (!scrolltop) {
                    return false;
                }
                let scrolltopbtn = $(".scrolltopa");
                let scrollHeight = parseInt(document.body.getBoundingClientRect().top);
                let basew = parseInt(ft.clientWidth);
                let sw = scrolltop.clientWidth;
                if (basew < 1000) {
                    let left = parseInt(fetchOffset(ft)['left']);
                    left = left < sw ? left * 2 - sw : left;
                    scrolltop.style.left = (basew + left + 44) + 'px';
                } else {
                    scrolltop.style.left = 'auto';
                    scrolltop.style.right = 0;
                }
                if (scrollHeight < -100) {
                    scrolltopbtn.addClass("scrolltopashow");
                } else {
                    scrolltopbtn.removeClass("scrolltopashow");
                }
            }
        }
        showTopLink();
        dlg("Animate go to top buttom actived.");
    }
    if (conf.pinnedTopBar) {
        // 添加固定顶栏样式
        appendStyle(
 `#toptb {
    position: fixed;
    width: 100%;
    z-index: 790;
    top: 0;
    box-shadow: rgba(0, 0, 0, 0.3) 3px 3px 5px 1px;
    min-width: 510px;
}

.new_wp {
    max-width: 1130px;
    width: 100%;
}

.mc_map_wp {
    padding-top: 45px;
}

.mw {
    padding-top: 60px
}

#user_info_menu, #myprompt_menu, #usertools_menu, #sslct_menu, #scbar_type_menu {
    position: fixed!important;
    top: 47px!important
}

#scbar_type_menu {
    top: 38px!important;
}

#e_controls {
    z-index: 790!important
}

@media screen and (max-width: 860px) {
    #toptb .z.light {
        display: none;
    }
    #toptb>.new_wp>.y {
        float: none;
        margin-left: 12px;
    }
}`);
        $(() => {
            // 重写editorcontrolpos函数,与固定顶栏兼容
            editorcontrolpos = () => {
                if (editorisfull) {
                    return;
                }
                let scrollTop = Math.max(document.documentElement.scrollTop, document.body.scrollTop);
                if (scrollTop + 47 > editorcontroltop && editorcurrentheight > editorminheight) {
                    $("#" + editorid + '_controls').prop("style", "z-index:0!important").css("position", 'fixed').css("top", '47px').css("width", editorcontrolwidth + 'px');
                    $("#" + editorid + '_controls_mask').css("display", '');
                } else {
                    $("#" + editorid + '_controls').css("position", '').css('top', '').css('width', '');
                    $("#" + editorid + '_controls_mask').css('display', 'none');
                }
            };
            //增加一个5px的遮罩,防止鼠标经过空隙时碰到底层内容
            $("#toptb").after('<div style="position: fixed;top: 47px;height: 5px;width: 100%;z-index:700;"></div>');
        });
        dlg("Pinned top bar style appended.");
    } else {
        $("head").append(`<style id="noFixedTopBar">.mc_map_wp {padding-top: 0px!important;}#toptb {position: relative;box-shadow: none;z-index:unset!important;}</style>`);
        dlg("Restored topbar.");
    }
    if (conf.fixTopBarPopMenu) {
        // 添加弹出菜单美化样式
        appendStyle(
`.p_pop:not(.blk) a {
    border-radius: 5px;
    border-bottom: none;
}

div#user_info_menu {
    margin-top: 5px;
}

.user_info_menu_info>li {
    margin-top: 2px;
}

a.rank {
    padding: 2px 7px!important;
    border-radius: 14px;
}

a.rank:hover {
    text-decoration: none;
}

ul.user_info_menu_btn {
    padding-top: 6px;
}

ul.user_info_menu_btn>li>a:hover {
    background:

}

ul.user_info_menu_btn>li>a {
    padding: 5px 8px;
    border-radius: 5px;
}

ul.user_info_menu_btn>li>a[onclick]:hover {
    background: red!important;
}

#myprompt_menu {
    margin-left: -10px;
}

#myprompt_menu, #usertools_menu, #sslct_menu {
    z-index: 791!important;
    margin-top: 5px!important;
    transform: translateX(-50%);
    margin-left: 20px;
}

.p_pop:not(.h_pop) {
    border: 1px solid #d1d1d1;
    min-width: unset;
    border-radius: 5px;
}

#myprompt_menu>li>a, #usertools_menu>li>a, #scbar_type_menu>li>a {
    border: none;
    border-radius: 5px;
    text-align: center;
    padding: 3px 15px;
}

#myprompt_menu>li>a:hover, #scbar_type_menu>li>a:hover, #usertools_menu>li>a:hover {
    background: #36b030;
    color: white;
}

div#sslct_menu {
    margin-left: 54px;
    padding-left: 14px;
}

.sslct_btn {
    border: none!important;
    width: 15px;
    height: 15px;
    padding: 2px;
}

.sslct_btn i {
    border-radius: 50%;
    width: 13px;
    height: 13px;
}

#scbar_type_menu {
    background: url(https://www.mcbbs.net/template/mcbbs/image/bg-wool-white.png);
}

a#scbar_type:after {
    content: "▼";
    margin-left: 10px;
}

#scbar_type_menu>li>a {
    padding: 3px 5px;
    line-height: 20px;
    height: 20px;
}

.scbar_type_td {
    background: url(https://www.mcbbs.net/template/mcbbs/image/scbar_txt.png) -95px center no-repeat
}

.y_search {
    width: 249px;
    border-radius: 3px;
    overflow: hidden;
}
.y_search_inp {
    float: unset;
}

#scbar_txt {
    width: 130px;
    background-color: transparent;
}
body.winter{
    --MExtBtnClr: #5c8dff!important;
}
body.nether{
    --MExtBtnClr: #a42e0b!important;
}
body{
    --MExtBtnClr: #36b030!important;
}
.user_info_menu_info li a.rank, ul.user_info_menu_btn>li>a:hover, .p_pop:not(.blk) a:hover,#myprompt_menu>li>a:hover, #scbar_type_menu>li>a:hover, #usertools_menu>li>a:hover, .p_pop:not(.blk) a:hover {
    background: var(--MExtBtnClr);
    color: white;
}`);
        dlg("Pop menu fix style appended.");
        // 重写extstyle函数,使更换主题时同步更新样式
        let __extstyle = extstyle;
        let checkStyle = (style = null) => {
            let theme = style == null ? getcookie('extstyle') : style;
            if (theme == "./template/mcbbs/style/winter") {
                $("body").removeClass("nether").addClass("winter");
            } else if (theme == "./template/mcbbs/style/default") {
                $("body").removeClass("winter nether");
            } else {
                $("body").addClass("nether").removeClass("winter");
            }
        }
        extstyle = (style) => {
            __extstyle(style);
            checkStyle(style);
        }
        checkStyle();
        dlg("Overwrite extstyle function");
    }
    if (conf.hoverableMedal) {
        // 重写勋章结构函数
        let rewriteMedal = () => {
            // 遍历所有未重写楼层
            $('.md_ctrl:not([glassmedal])').attr("glassmedal", "true").each((t, v) => {
                // 遍历楼层所有勋章
                $(v).children(0).children('img').each((b, n) => {
                    // 获得勋章ID
                    let id = 'md' + /\_\d*$/.exec(n.id)[0];
                    // 重写勋章结构
                    $(v).append(
                        $('<span class="hoverable-medal" id="' + n.id + '" style="background-image:url(' + n.src + ')"></span>').on('mouseover', () => {
                            showMenu({
                                'ctrlid': n.id,
                                'menuid': id + '_menu',
                                'pos': '12!'
                            });
                        })
                    );
                    // 重写提示样式
                    $("#" + id + "_menu .tip_horn").css("background-image", "url(" + n.src + ")");
                    // 移除旧的勋章
                    n.remove();
                });
            });
            dlg("Hoverable medal rewrote.");
        };
        //调用重写勋章函数
        $(rewriteMedal);
        // 在Ajax时重新调用Ajax函数,保存勋章样式
        $(this).on("DiscuzAjaxGetFinished DiscuzAjaxPostFinished", rewriteMedal);
        // 添加勋章样式
        appendStyle(
`
.hoverable-medal:hover:after {
    margin-top: 0px!important;
    opacity: 1!important;
}

.hoverable-medal:after {
    display: block;
    content: '';
    margin-top: -15px;
    opacity: 0.6;
    transition-duration: .4s;
    background-image: url(` + staticRes.medalReflectImage + `);
    width: 100%;
    height: 100%;
    filter: blur(2px);
}

div.tip.tip_4[id*=md_] {
    width: 105px;
    height: 165px;
    border: none;
    box-shadow: black 0px 2px 10px -3px;
    margin-left: 38px;
    margin-top: 115px;
    background: black;
    overflow: hidden;
    pointer-events: none!important;
    border-radius: 5px;
    padding: 0px;
}

div.tip.tip_4[id*=md_] .tip_horn {
    background-size: cover;
    background-position: center;
    height: 200%;
    width: 200%;
    z-index: -1;
    filter: blur(7px) brightness(0.8);
    top: -50%;
    left: -50%;
}

div.tip.tip_4[id*=md_] .tip_c {
    color: rgba(255, 255, 255, 0.98);
}

div.tip.tip_4[id*=md_] h4 {
    text-align: center;
    padding: 10px 5px;
    background-color: rgba(255, 255, 255, 0.3);
}

div.tip.tip_4[id*=md_] p {
    padding: 0px 10px;
    position: absolute;
    top: calc(50% + 38px);
    transform: translateY(calc(-50% - 26px));
}

.md_ctrl {
    margin-left: 17px!important;
    padding-bottom: 15px;
}

.hoverable-medal {
    width: 31px;
    height: 53px;
    transition-duration: 0.4s;
    border-radius: 3px;
    display: inline-block;
    margin: 5px;
    background-position: center;
    box-shadow: 0px 2px 5px 0px black;
    overflow: hidden;
}

.hoverable-medal:hover {
    transform: matrix3d(1, 0, 0, 0, 0, 1, 0, -0.003, 0, 0, 1, 0, 0, -1.5, 0, 0.9);
    box-shadow: 0px 2px 10px -3px black;
}

.pg_medal .mgcl img {
    margin-top: 12px!important
}

.mg_img {
    box-shadow: inset 0 0 10px 4px rgba(0, 0, 0, 0.3);
    border-radius: 5px;
}`);
        dlg("Hoverable medal style appended.");
    }
    if (conf.quickAtList) {
        appendStyle(
`#fastpostatList.in_editorbtn, #postatList {
    background-size: 54px;
    background-position: -23px 3px;
}

#fastpostatList, #postatList {
    background-image: url(` + staticRes.atBtnImage + `);
    background-size: 50px;
    background-position: -6px 2px;
}`);
        // 获得At代码函数
        let getAtCode = () => {
            // 分隔list
            let quickAtList = conf.quickAtList.split(",");
            let atstr = "";
            //拼接@代码
            $(quickAtList).each((i, v) => {
                atstr += "@" + v + " ";
            });
            return atstr;
        }
        // 将函数暴露到全局
        MExt_Func_getAtCode = getAtCode;
        // 监听按键事件
        $(document).on("keydown", (e) => {
            if (e.shiftKey && e.ctrlKey && e.keyCode == 65) {
                // 判断是否在输入框内
                if (($(document.activeElement).prop("nodeName") == "INPUT" && $(document.activeElement).prop("type") == "text")) {
                    // 拼接方法插入
                    $(document.activeElement).val($(document.activeElement).val() + getAtCode());
                    dlg("@ string added");
                } else if ($(document.activeElement).prop("nodeName") == "TEXTAREA") {
                    // discuz内建函数插入
                    seditor_insertunit('fastpost', getAtCode(), '');
                    dlg("@ string added");
                }
            }
        });
        // 高级编辑模式插入@代码
        $(() => {
            if ($("#e_iframe").length) {
                // 由于高级模式的输入框是iFrame,无法直接监听,故再次监听高级输入框的按键事件
                $($("#e_iframe")[0].contentWindow).on("keydown", (e) => {
                    if (e.shiftKey && e.ctrlKey && e.keyCode == 65) {
                        // 判断是否在输入框内
                        if ($(document.activeElement).prop("nodeName") == "IFRAME") {
                            //discuz内建函数插入
                            insertText(getAtCode());
                            dlg("@ string added");
                        }
                    }
                });
            }
        });
        let hookReplyBtn = () => {
            if ($("#postatList").length > 0) { return false; }
            $("#postat.fat").after('<a id="postatList" href="javascript:;" title="快速@" onclick="seditor_insertunit(\'post\',MExt_Func_getAtCode(), \'\');">快速@</a> ');
            dlg("Reply bottons appends.");
        }
        $("#append_parent").on('DOMNodeInserted', hookReplyBtn);
        $(() => {
            $("#fastpostat").after('<a id="fastpostatList" href="javascript:;" title="快速@" class="" onclick="seditor_insertunit(\'fastpost\',MExt_Func_getAtCode(), \'\');">快速@</a> ');
            $("#e_adv_s1").append('<a id="fastpostatList" href="javascript:;" title="快速@" class="in_editorbtn" onclick="insertText(MExt_Func_getAtCode());">快速@</a>');
        });

    }
    if (conf.viewWarns) {
        // 添加查看警告样式
        appendStyle(
`.view_warns_inposts {
    background: url(template/mcbbs/image/warning.gif) no-repeat 0px 2px;
    background-size: 16px;
    width: 90px!important;
}

.view_warns_home a {
    background: url(template/mcbbs/image/warning.gif) no-repeat 1px 2px!important;
    background-size: 16px!important;
}`);
        // 添加查看警告按钮函数
        let addVWLink = () => {
            $(".plhin").each((i, v) => {
                let href = $(v).find(".authi .xw1").attr("href");
                if (!href) {
                    return false;
                }
                let uid = /uid=(\d*)/.exec(href)[1];
                $(v).find("ul.xl.xl2.o.cl:not([vw_added*=true])").attr("vw_added", "true").append($('<li class="view_warns_inposts"><a href="forum.php?mod=misc&action=viewwarning&tid=952104&uid=' + uid + '" title="查看警告记录" class="xi2" onclick="showWindow(\'viewwarning\', this.href)">查看警告记录</a></li>'));
            });
            dlg("In-posts view warns link added");
        }
        // 在DiscuzAjax时重新调用添加函数,防止失效
        $(this).on("DiscuzAjaxGetFinished", addVWLink).on("DiscuzAjaxPostFinished", addVWLink);
        dlg("adddVWLink Ajax Event attached.");
        $(() => {
            // 添加查看警告按钮
            addVWLink();
            // 用户信息界面添加查看警告按钮
            let href = $("#uhd .cl a").attr("href");
            if (!href) {
                return false;
            }
            let uid = /uid=(\d*)/.exec(href)[1];
            if (!uid) {
                return false;
            }
            $("#uhd .mn ul").append('<li class="view_warns_home"><a href="forum.php?mod=misc&action=viewwarning&tid=952104&uid=' + uid + '" title="查看警告记录" class="xi2" onclick="showWindow(\'viewwarning\', this.href)">查看警告记录</a></li>');
            dlg("Home page view warns link added.")
        });
    }
    // 自定义举报内容
    if (conf.myReportReason) {
        // 获得举报内容列表函数
        let getReasons = () => {
            // 分隔list
            let reportReason = conf.myReportReason.split("\n");
            let rrstr = "<p class=\"mtn mbn\">";
            //拼接HTML
            $(reportReason).each((i, v) => {
                rrstr += '<label><input type="radio" name="report_select" class="pr" onclick="$(\'report_other\').style.display=\'none\';$(\'report_msg\').style.display=\'none\';$(\'report_message\').value=\'' + v + '\'" value="' + v + '"> ' + v + '</label><br>';
            });
            rrstr += "</p>";
            return rrstr;
        }
        // 举报按钮钩子函数
        let hookReportBtn = () => {
            if ($("#report_reasons[appended]").length > 0) { return false; };
            let reportContent = getReasons();
            $("#report_reasons").attr("appended", "true").before(reportContent);
        }
        $("#append_parent").on('DOMNodeInserted', hookReportBtn);
        $(this).on("DiscuzAjaxGetFinished DiscuzAjaxPostFinished", hookReportBtn);
    }
    // 移除外链警告延时,直接跳转目标页
    if (conf.removeLinkWarn) {
        if (location.pathname == "/plugin.php" && getRequest('id') == "link_redirect") {
            let url = getRequest('target');
            if (url) {
                // 跳就完事了
                location.href = decodeURIComponent(url);
            }
        }
    }
    if (conf.useIgInQuickReply) {
        // 快速回复框使用个人签名
        let hookReplyBtn = () => {
            if ($("#fwin_reply #usesig").length > 0) { return false; }
            $("#fwin_reply #postsubmit").after('<label for="usesig" style="margin-left: 10px;float: left;margin-top: 3px;"><input type="checkbox" name="usesig" id="usesig" class="pc" value="1" checked="checked">使用个人签名</label>');
        }
        $("#append_parent").on('DOMNodeInserted', hookReplyBtn);
        $(() => {
            // 底部快速回复增加选项
            $("#fastpostsubmit").after('<label for="usesig" style="margin-left: 10px;"><input type="checkbox" name="usesig" id="usesig" class="pc" value="1" checked="checked">使用个人签名</label>');
        });
    }
    // 故意把这个功能搞玄乎,纯粹是好玩,其实还是非常简单的.
    // 这里包含一些对体验影响不大又非常杂的修复选项,不适合单独开一个选项,显得很臃肿.
    // 配置里的这一串数字代表每一个功能的开关情况,比如第一位数字为1时代表第一个功能开启,第二位为0时,代表第二个功能关闭.
    if (/^[01]*$/.exec(conf.miscFix)) {
        let fixconf = conf.miscFix.split("");
        let fixlist = [
            // 暗牧悬浮预览
            { "style": ".t_f font[style*=\"background-color:black\"], .t_f font[style*=\"background-color:#000\"] {transition-duration: .3s;transition-delay: .5s;cursor: default;}.t_f font[style*=\"background-color:black\"]:hover, .t_f font[style*=\"background-color:#000\"]:hover {transition-delay: 0s;background-color: transparent!important;}" },
            //增加空方法,用于修复论坛的一个报错.
            { "script": "announcement = () => {};relatekw = ()=>{};" },
            //修复页脚问题
            { "style": ".mc_map_wp{min-height:calc(100vh - 202px)!important;}" },
            //修复用户组页面不对齐的问题
            { "style": ".tdats .tb{margin-top:0px}" },
            // 修复编辑器@超级加倍的问题
            { "script": "$(()=>{if(typeof setEditorEvents != \"undefined\"){let __setEditorEvents = setEditorEvents;setEditorEvents= ()=>{ __setEditorEvents();setEditorEvents=()=>{};}}})" },
            // 允许改变个人签名编辑框大小
            { "style": "#sightmlmessage{resize:vertical;}" },
            // 按住shift点击带有超链接的图片则不打开链接
            { "script": `$(()=>{$("img.zoom").parent().each((i,v)=>{if(v.nodeName=="A"){$(v).on("click",(e)=>{console.log(e);if(e.shiftKey){e.preventDefault();}})}})})` },
            // 修复某些页面书框被撕裂的问题
            { "script": "$(()=>{if(!$('.mc_map_wp .mc_map_border_foot').length){$('.mc_map_border_foot').remove();$('.mc_map_wp').append('<div class=\"mc_map_border_foot\"></div>')}})" },
            // 默认使用高清头像
            { "script": "$(()=>{$('.avt img, .avtm img, .special_photo img').each((i,v)=>{v.src=v.src.replace('middle','big').replace('small','big')});})" },
            // 主动聚焦编辑器iframe,修复报错问题.
            { "script": "$(()=>{if(typeof wysiwyg !='undefined' && wysiwyg){editwin.document.body.focus()};})" },
            // 小提示样式修复
            { "style": ".pc_inner{padding-left:12px}" }
        ];
        let styleContent = "";

        $(fixlist).each((i, v) => {
            if (typeof fixconf[i] == "undefined") { fixconf[i] = "1" }
            if (fixconf[i] === "1") {
                // 拼接样式字符串
                styleContent += fixlist[i].style ? fixlist[i].style : "";
                // 执行脚本
                eval(fixlist[i].script ? fixlist[i].script : "");
            }
        });
        // 添加修复样式
       appendStyle(styleContent)
    }
    if (conf.fixImgZoom) {
        dlg("Zoom style append");
        appendStyle(
`#img_scale {
    opacity: 0;
    position: absolute;
    right: 20px;
    bottom: 20px;
    background: #0006;
    transition-duration: .2s;
    color: white;
    padding: 10px;
    pointer-events: none;
    border-radius: 10px;
}

#imgzoom_zoom {
    height: auto;
    transition-duration: .2s
}

#imgzoom_zoomlayer {
    height: auto!important
}

#imgzoom {
    width: auto!important;
    height: auto!important
}`);
        dlg("FixImgZoom style appended.");
        let __zoom = zoom;
        let t = 0;
        // 初始化基本缩放信息对象
        let img = { width: 0, height: 0, top: 0, left: 0, radio: 1, scale: 1, orgwidth: 0 };
        // 缩放函数
        let resize = (width) => {
            dlg("Image resizing...")
            clearTimeout(t);
            // 显示缩放比例
            $("#img_scale").html(parseInt(img.scale * 100) + "%").css("opacity", 1);
            t = setTimeout(() => { $("#img_scale").css("opacity", 0) }, 2000);
            // 计算目标大小和位置
            let ow = img.width;
            img.width = width;
            ow = (ow - img.width) / -2;
            img.left -= ow;
            img.top -= ow * img.radio;
            // 修改
            $("#imgzoom_zoom").css("width", img.width + "px");
            $("#imgzoom").css("left", img.left + "px");
            $("#imgzoom").css("top", img.top + "px");
        }
        // 保存基本信息
        let initP = () => {
            dlg("Init Picture info");
            img.width = parseInt($("#imgzoom_zoom").attr("width"));
            img.height = parseInt($("#imgzoom_zoom").attr("height"));
            img.radio = img.height / img.width;
            img.top = parseInt($("#imgzoom").css("top"));
            img.left = parseInt($("#imgzoom").css("left"));
            img.scale = 1;
            img.orgwidth = img.width;
        }
        zoom = (obj, zimg, nocover, pn, showexif) => {
            // 伪装成IE,使原函数的DOMMouseScroll事件监听器以可以被卸除的形式添加
            BROWSER.ie = 6;
            __zoom(obj, zimg, nocover, pn, showexif);
            // 防止翻车,改回去
            setTimeout(() => { BROWSER.ie = 0; dlg("IE canceled.") }, 1000);
            // 等待窗口出现
            let wait = setInterval(() => {
                if ($("#imgzoom_zoom").length) {
                    dlg("Image found");
                    clearInterval(wait);
                    // 信息归零,准备下一次保存
                    img = { width: 0, height: 0, top: 0, left: 0, radio: 1, scale: 1, orgwidth: 0 };
                    // 显示遮罩
                    $("#imgzoom_cover").css("display", "unset");
                    // 判断是否已经监听事件,防止超级加倍
                    if ($("#imgzoom").attr("fixed") == "true") { return true; }
                    // 原始尺寸按钮事件
                    $("#imgzoom_adjust").on("click", () => {
                        dlg("return source size");
                        $("#imgzoom").css("transition-property", "opacity,top,left,transform");
                        img.width == 0 ? initP() : 0;
                        img.scale = 1;
                        resize($("#imgzoom_zoom").attr("width"));
                    });
                    // 屏蔽页面滚动
                    $("#imgzoom_cover").on("mousewheel DOMMouseScroll", (e) => {
                        if (e.ctrlKey || e.altKey || e.shiftKey) { return true; }
                        e.preventDefault();
                    });
                    // 卸除原函数监听器
                    $("#imgzoom")[0].onmousewheel = null;
                    // 增加显示缩放大小元素并监听事件
                    $("#imgzoom").append(`<span id="img_scale"></span>`).on("mousewheel DOMMouseScroll", (e) => {
                        // 判断是否按下功能键
                        if (e.ctrlKey || e.altKey || e.shiftKey) { dlg("Func key pressed."); return true; }
                        // 阻止滚动
                        e.preventDefault();
                        // 兼容火狐,正确判断滚轮方向
                        let scroll = e.originalEvent.wheelDelta ? e.originalEvent.wheelDelta : -e.originalEvent.detail;
                        // 忽略无效滚动
                        if (scroll == 0) { return true; }
                        // 判断是否需要初始化
                        img.width == 0 ? initP() : 0;
                        // 规定需要显示过渡动画的属性
                        $("#imgzoom").css("transition-property", "opacity,top,left,transform");
                        // 判断是否过小
                        if (scroll < 0 && ((img.width < 350 && img.radio < 1) || (img.width * img.radio < 350 && img.radio >= 1))) {
                            // 回弹动画
                            dlg("Reach min size");
                            $("#imgzoom").css("transform", "scale(0.8)");
                            setTimeout(() => { $("#imgzoom").css("transform", "scale(1)"); }, 200);
                            return true;
                        }
                        // 修改缩放比例
                        img.scale += scroll > 0 ? 0.1 : -0.1;
                        // 判断比例是否过小
                        if (img.scale < 0.1) {
                            img.scale = 0.1;
                            // 回弹动画
                            dlg("Reach min size");
                            $("#imgzoom").css("transform", "scale(0.8)");
                            setTimeout(() => { $("#imgzoom").css("transform", "scale(1)"); }, 200);
                            return true;
                        }
                        // 缩放
                        resize(img.orgwidth * Math.pow(img.scale, 2));
                    }).attr("fixed", "true");
                    // 按下鼠标事件
                    $("#imgzoom").on("mousedown", (e) => {
                        // 按下鼠标时移除修改位置的过渡动画,使窗口跟手
                        dlg("Animate removed");
                        $("#imgzoom").css("transition-property", "opacity");
                    });
                    // 释放鼠标事件
                    $("#imgzoom").on("mouseup", (e) => {
                        // 改回去
                        $("#imgzoom").css("transition-property", "opacity,top,left,transform");
                        // 保存移动后的窗口位置
                        img.top = parseInt($("#imgzoom").css("top"));
                        img.left = parseInt($("#imgzoom").css("left"));
                        dlg("Animate added,Pos saved");
                    });
                }
            }, 50);
        }
        dlg("Zoom function rewrote.");
    }
    if (conf.myCSS) {
        // 增加自定义的CSS
        dlg("DIY CSS Appended");
        $("head").append(`<style id="myCSS">` + conf.myCSS + `</style>`);
    }
    if (conf.disableAutoplay) {
        let clearAutoPlay = () => {
            dlg("Canceling autoplay");
                $("iframe[id*=iframe_mp3]:not([id*=no_autoplay])").each((i, v) => {
                    // 去掉自动播放属性
                    let player = document.createElement("iframe");
                    let hidden = document.createElement("div");
                    hidden.id = v.id;
                    hidden.style.display = "none";
                    player.id = v.id+"_no_autoplay";
                    player.width = v.width;
                    player.height = v.height;
                    player.frameBorder = v.frameBorder;
                    player.allow = v.allow;
                    player.src = v.src.replace("&auto=1", "");
                    v.after(hidden);
                    v.after(player);
                    v.remove();
                });
        };
        $(this).on("DiscuzAjaxGetFinished DiscuzAjaxPostFinished", () => { setTimeout(clearAutoPlay, 100); });
        $(clearAutoPlay);
    }
    if (conf.myLinks) {
        // 分割
        $(conf.myLinks.split("\n")).each((i, v) => {
            try {
                //判断是否合法
                if (!v.split(" ")[1] || !v.split(" ")[0]) { return true; }
                // 添加
                $(() => { $("#usertools_menu").append('<li><a href="' + v.split(" ")[1] + '">' + v.split(" ")[0] + '</a></li>') });
            } catch (ignore) { }
        });
        dlg("My Links added");
    }
    if (conf.maxMedalLine >= 0) {
        let restrict = () => {
            // 判断是否在个人主页
            if ($("#uhd").length > 0) { $("#restrictMedalLine").remove(); return; }
            // 限制勋章行数
            dlg("Restricting line...");
            $('.md_ctrl:not([restrictline])').attr("restrictline", "true").append($("<span class=\"toggle-all\">展开/收起勋章</span>").on("click", (e) => { $(e.target).parent().toggleClass("show-all") })).each((i, v) => {
                if ((v.childElementCount - 2 <= conf.maxMedalLine * 3 && conf.hoverableMedal) || (v.firstChild.childElementCount - 2 <= conf.maxMedalLine * 3 && !conf.hoverableMedal)) {
                    v.removeChild(v.lastChild);
                }
            });
        }
        let maxheight = conf.maxMedalLine * (conf.hoverableMedal ? 60 : 55) + 45;
        $(restrict);
        $(this).on("DiscuzAjaxGetFinished DiscuzAjaxPostFinished", restrict);
        appendStyle(
`.md_ctrl span.toggle-all {
    width: 125px;
    display: block;
    position: absolute;
    bottom: 0;
    text-align: center;
    left: 0;
    padding: 30px 0px 5px 0px;
    background-image: linear-gradient(0deg, #e3c99e, #e3c99e, transparent);
    color: #3e6c99;
    cursor: pointer;
    user-select: none;
}

.md_ctrl {
    position: relative;
    overflow-y: hidden;
    max-height: ` + maxheight.toString() + `px;
}

.md_ctrl.show-all {
    max-height: 3000px;
    padding-bottom: 40px;
}`)
        dlg("Appended Restrict Medal Line style");
    }
    if (conf.remenberEditMode) {
        if (localStorage.getItem("MExt_EditMode") === null) {
            localStorage.setItem("MExt_EditMode", "false");
        }
        $(() => {
            dlg("Remenber Editor Mode actived.");
            $("#e_switchercheck").on("click", (e) => {
                dlg("Editor mode switch.");
                localStorage.setItem("MExt_EditMode", e.currentTarget.checked.toString());
            });
            if (localStorage.getItem("MExt_EditMode") == "true") {
                dlg("Switch editor mode");
                $("#e_switchercheck").click();
            }
        });
    }
    if (conf.highlightThreads) {
        appendStyle(
`.tl .icn {
    background-color: rgba(200, 200, 200, 0.3)!important;
    background-image: linear-gradient(-90deg, rgb(251 242 219), transparent);
    border-left: 3px solid rgb(200, 200, 200);
    transition-duration: .2s;
}
.tl .icn.newReply {
    background-color: rgba(255, 136, 0, 0.3)!important;
    border-left: 3px solid rgb(255, 136, 0);
}

.tl .icn.newMember {
    background-color: rgba(110, 232, 115, 0.3)!important;
    border-left: 3px solid rgb(110, 232, 115);
}

.tl .icn.hotThread {
    background-color: rgba(235, 132, 132, 0.3)!important;
    border-left: 3px solid rgb(235, 132, 132);
}

.tl .icn.digest {
    background-color: rgba(0, 203, 214, 0.3)!important;
    border-left: 3px solid rgb(0, 203, 214);
}

.tl .icn.digest2 {
    background-color: rgba( 0, 161, 204, 0.3)!important;
    border-left: 3px solid rgb( 0, 161, 204);
}

.tl .icn.digest3 {
    background-color: rgba(0, 123, 194, 0.3)!important;
    border-left: 3px solid rgb(0, 123, 194);
}

.tl .icn.close {
    background-color: rgba(187, 187, 187, 0.3)!important;
    border-left: 3px solid rgb(187, 187, 187);
}

.tl .icn.forumSticker {
    background-color: rgba(161, 215, 252, 0.3)!important;
    border-left: 3px solid rgb(161, 215, 252);
}

.tl .icn.partSticker {
    background-color: rgba(110, 171, 235, 0.3)!important;
    border-left: 3px solid rgb(110, 171, 235);
}

.tl .icn.globalSticker {
    background-color: rgba(33, 106, 207, 0.3)!important;
    border-left: 3px solid rgb(33, 106, 207);
}

.tl .icn.poll {
    background-color: rgba(250, 123, 147, 0.3)!important;
    border-left: 3px solid rgb(250, 123, 147);
}

.tl .icn.debate {
    background-color: rgba(0, 153, 204, 0.3)!important;
    border-left: 3px solid rgb(0, 153, 204);
}`);
        let highlighting = () => {
            $('#moderate a[title*="有新回复"]').parent().addClass("newReply");
            $('#moderate img[alt="新人帖"]').parent().parent().children(".icn").addClass("newMember");
            $('#moderate img[alt="热帖"]').parent().parent().children(".icn").addClass("hotThread");
            //精华
            $('#moderate img[alt="digest"]').parent().parent().children(".icn").addClass("digest");
            $('#moderate img[title="精华 2"]').parent().parent().children(".icn").addClass("digest2");
            $('#moderate img[title="精华 3"]').parent().parent().children(".icn").addClass("digest3");
            $('#moderate a[title*="关闭的主题"]').parent().addClass("close");
            $('#moderate a[title*="本版置顶主题"]').parent().addClass("forumSticker");
            $('#moderate a[title*="分类置顶主题"]').parent().addClass("partSticker");
            $('#moderate a[title*="全局置顶主题"]').parent().addClass("globalSticker");
            $('#moderate a[title*="辩论"]').parent().addClass("debate");
            $('#moderate a[title*="投票"]').parent().addClass("poll");
            $('#moderate a[title*="悬赏"]').parent().addClass("newReply");
            $('#moderate a.s.xst[style*=color]').each((i,v)=>{v.parentNode.previousElementSibling.style.setProperty("background-color",v.style.color.replace(")",",0.4)").replace("rgb(","rgba("),"important");v.parentNode.previousElementSibling.style.borderLeftColor=v.style.color;});
        };
        $(highlighting);
        let waiter = 0;
        $(() => {
            let nxBtn = $("#autopbn");
            nxBtn.on("click", () => {
                if (waiter == 0) {
                    waiter = setInterval(() => {
                        if (nxBtn.text() != "正在加载, 请稍后...") {
                            clearInterval(waiter);
                            waiter = 0;
                            highlighting();
                        }
                    }, 100);
                }
            });
        });
    }
    if (conf.fixAnchor) {
        appendStyle(
`table.plhin td.t_f span[id]:not([id^=anchor_]), .fastpreview span[id]:not([id^=anchor_]) {
    display: none;
}`);
        let fix = () => {
            $("table.plhin td.t_f span[id]:not([id^=anchor_]),.fastpreview span[id]:not([id^=anchor_])").each((i, v) => {
                $("a[href$=#" + v.id + "]").each((i, v) => {
                    v.href.replace("#" + v.id, "#anchor_" + v.id);
                });
                v.id = "anchor_" + v.id;
            });
        }
        $(fix);
        $(this).on("DiscuzAjaxGetFinished DiscuzAjaxPostFinished", fix);
    }
    if(conf.replaceFlash){

        $("span[id*=swf] embed").each((i,v)=>{
            let player = document.createElement("iframe");
            player.src = v.src.replace("style/swf/widget.swf","outchain/player").replace("sid=","id=");
            player.width = v.width;
            player.height = v.height;
            player.frameBorder = "no";
            player.allow = "autoplay; fullscreen";
            player.id = v.parentElement.id + "_no_autoplay";
            v.parentElement.after(player);
            v.parentElement.remove();
        });
    }
    if(conf.quickRainbow){
        appendStyle(
`

#fastpostrainbow, #postrainbow,#e_rbn_s1 {
    background-image: url(` + staticRes.rainbowBtnImage + `);
    background-size: 28px;
    background-position: center top;
}
#fastpostrainbow.in_editorbtn , #postrainbow {
    background-size: 16px;
    background-position: center;
}`);
        let rainbowFast = ()=>{
            let target = document.getElementById("fastpostmessage");
            if(target.selectionStart != target.selectionEnd){
                let str = target.value.substr(target.selectionStart,target.selectionEnd);
                seditor_insertunit('fastpost',gencode(str,0),'');
            }
        };
        let rainbowFloat = ()=>{
            let target = document.getElementById("postmessage");
            if(target.selectionStart != target.selectionEnd){
                let str = target.value.substr(target.selectionStart,target.selectionEnd);
                seditor_insertunit('post',gencode(str,0),'');
            }
        };
        let rainbow = ()=>{
            if(getSel()==""){
                return;
            }
            addSnapshot(getEditorContents());
            insertText(gencode(getSel(),wysiwyg));
        };
        let hookReplyBtn = () => {
            if ($("#postrainbow").length > 0) { return false; }
            let btn = document.createElement("a");
            btn.id = "postrainbow";
            btn.href = "javascript:;";
            btn.title = "彩虹文字";
            btn.addEventListener("click",rainbowFloat);
            btn.innerText = "彩虹文字";
            $("#postat.fat").after(btn);
            dlg("Reply bottons appends.");
        }
        $("#append_parent").on('DOMNodeInserted', hookReplyBtn);
        $(() => {
            let btn = document.createElement("a");
            btn.id = "fastpostrainbow";
            btn.href = "javascript:;";
            btn.title = "彩虹文字";
            btn.className = "in_editorbtn";
            btn.addEventListener("click",rainbowFast);
            btn.innerText = "彩虹文字";
            $("#fastpostat").after(btn);
            let btn2 = document.createElement("a");
            btn2.id = "e_rbn_s1";
            btn2.href = "javascript:;";
            btn2.title = "彩虹文字";
            btn2.addEventListener("click",rainbow);
            btn2.innerText = "彩虹文字";
            $("#e_adv_s1").append(btn2);
        });
        let nextColor = (clr,step)=>{
                if(clr.r==255 && clr.b != 255){
                    clr.g -= step;
                } else if(clr.g==255 && clr.r != 255){
                    clr.b -= step;
                }else if(clr.b == 255 && clr.g != 255){
                    clr.r -= step;
                }
                while(clr.r > 255 || clr.r < 0 || clr.g > 255 || clr.g< 0 || clr.b > 255 || clr.b < 0){
                    if(clr.r > 255){
                        clr.g += 255 - clr.r;
                        clr.r = 255;
                        continue;
                    }
                    if(clr.g < 0){
                        clr.b -= clr.g;
                        clr.g = 0;
                        continue;
                    }
                    if(clr.b > 255){
                        clr.r += 255 - clr.b;
                        clr.b = 255;
                        continue;
                    }
                    if(clr.r < 0){
                        clr.g -= clr.r;
                        clr.r = 0;
                        continue;
                    }
                    if(clr.g > 255){
                        clr.b += 255 - clr.g;
                        clr.g = 255;
                        continue;
                    }
                    if(clr.b < 0){
                        clr.r -= clr.b;
                        clr.b = 0;
                        continue;
                    }
                }
            return clr;
        }
        let dCode = (str)=>{
            while(str.length<2){
                str = "0"+str;
            }
            return str;
        }
        let HexC = (color)=>{
            return "#"+dCode(parseInt(color.r).toString(16))+dCode(parseInt(color.g).toString(16))+dCode(parseInt(color.b).toString(16));
        }
        let gencode = (str,type)=>{
            let color = {
                "r": 255,
                "g": 0,
                "b": 0
            }
            let len = str.length;
            let step = 1530 / len < 1 ? 1 : 1530 / len;
            let rstr = '';
            for(let i = 0;i<len;i++){
                if(type==0){
                    rstr += "[color="+HexC(color)+"]"+str.charAt(i)+"[/color]";
                }else{
                    rstr+="<font color=\""+HexC(color)+"\">"+str.charAt(i)+"</font>";
                }
                color = nextColor(color,step);
            }
            return rstr;
        };
        }
})();